package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/24 21:13
 */
@RestController
@RequestMapping("/admin/category")
@CrossOrigin
public class DtsCategoryController {

    @Autowired
    private DtsCategoryService dtsCategoryService;

    /**
     * 商品类名添加
     * @param id
     * @param name
     * @param sort
     * @param order
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "查询")
    @RequiresPermissions(value = "admin:category:list")
    @GetMapping("/list")
    public Object list(Integer id,
                       String name,
                       String sort,
                       String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit){
        IPage<DtsCategory> ipage = dtsCategoryService.findListByQuery(id,name,sort,order,page,limit);
        Map data = new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * /create
     */
    @RequiresPermissions(value = "admin:category:list")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsCategory dtsCategory){
        dtsCategory = dtsCategoryService.create(dtsCategory);
        return ResponseUtil.ok(dtsCategory);
    }

    /**
     * update
     */
    @RequiresPermissions(value = "admin:category:update")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsCategory dtsCategory){
        dtsCategory = dtsCategoryService.update(dtsCategory);
        return ResponseUtil.ok(dtsCategory);
    }

    /**
     * delete
     */
    @RequiresPermissions(value = "admin:category:delete")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory dtsCategory){
        dtsCategory = dtsCategoryService.delete(dtsCategory);
        return ResponseUtil.ok(dtsCategory);
    }

}
